package com.wangchao.administrator.zenmechi.ui.loginactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wangchao.administrator.zenmechi.R;
import com.wangchao.administrator.zenmechi.ui.fragment.userfragment.userMsg;
import com.wangchao.administrator.zenmechi.ui.registactivity.RegistActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LoginActivity extends AppCompatActivity implements LoginContract.View,View.OnClickListener{

    @BindView(R.id.tv_top_login)
    TextView tvTopLogin;
    @BindView(R.id.tv_regist)
    TextView tvRegist;
    @BindView(R.id.tv_Name)
    TextView tvName;
    @BindView(R.id.et_userName)
    EditText etUserName;
    @BindView(R.id.tv_Pwd)
    TextView tvPwd;
    @BindView(R.id.et_passWord)
    EditText etPassWord;
    @BindView(R.id.tv_forget)
    TextView tvForget;
    @BindView(R.id.bt_Login)
    Button btLogin;

    private LoginPresenter presenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initListener();

        presenter=new LoginPresenter(this);
    }

    private void initListener() {
        btLogin.setOnClickListener(this);
        tvRegist.setOnClickListener(this);
        tvForget.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_Login:
              String name=etUserName.getText().toString().trim();
                if(!TextUtils.isEmpty(name)){
                    presenter.getLoginInfo(name);
                }else
                Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_regist:
                Intent regist=new Intent(this, RegistActivity.class);
                startActivity(regist);
                break;
            case R.id.tv_forget:
                Intent inRegist=new Intent(this, RegistActivity.class);
                startActivity(inRegist);
                break;
        }
    }

    @Override
    public void loginSuccess(String pwd) {
      String pass= etPassWord.getText().toString().trim();
        if(!TextUtils.isEmpty(pass)){
            if(pass.equals(pwd)){
                Toast.makeText(this,"登陆成功",Toast.LENGTH_SHORT).show();
                EventBus.getDefault().post(new userMsg(etUserName.getText().toString().trim()));
               finish();
            }else{
                Toast.makeText(this,"密码错误",Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(this,"密码不能为空！",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void loginFailed(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }
}
